package top.jacktgq.hsp.nio.buffer_channel;

import java.nio.ByteBuffer;

/**
 * @Author CandyWall
 * @Date 2020/11/11--13:36
 * @Description
 */
public class NIOByteBufferPutGet {
    public static void main(String[] args) {
        //创建一个ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);
        //类型化方式放入数据
        byteBuffer.putInt(100);
        byteBuffer.putLong(9);
        byteBuffer.putChar('强');
        byteBuffer.putShort((short) 4);

        //取出数据
        byteBuffer.flip();

        //错误的取法：不按照存入的类型取数据
        //会报如下错误：java.nio.BufferUnderflowException
        /*System.out.println(byteBuffer.getLong());
        System.out.println(byteBuffer.getLong());
        System.out.println(byteBuffer.getLong());*/

        //正确的取法：
        System.out.println(byteBuffer.getInt());
        System.out.println(byteBuffer.getLong());
        System.out.println(byteBuffer.getChar());
        System.out.println(byteBuffer.getShort());
    }
}
